package GUI;
import java.awt.BorderLayout;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JList;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.BoxLayout;
import javax.swing.JOptionPane;

import java.awt.Color;
import java.io.*;

import java.util.*;

import javax.swing.JLabel;
import javax.swing.JTextField;

import Log.Log;

import client.ClientConsole;



import java.awt.BorderLayout;


public class ClientSideGUI extends JPanel implements Runnable , ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JTextField PortInput;
	private JTextField IPInput;
	JPanel panel_1;
	JButton btnSet;
	JButton btnGet;
	public String IP;
	public boolean bDidntSetConnection = true;
	public int PORT;
	JButton btnConnect;
	ClientSideGUI frame;
	JPanel panel;
	JButton btnPing;
	private ClientConsole clientFrame;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					//ClientSideGUI frame = new ClientSideGUI();
					//frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ClientSideGUI() {
		//ClientSideGUI frame = new ClientSideGUI();
		 //frame = new ClientSideGUI();
		 	BuildGui();
			this.setVisible(true);
		
		//frame.setVisible(true);
		
	}
	public void actionPerformed(ActionEvent event) {
        //do stuff
		if (event.getSource()==btnPing)
		{
			if ((IPInput.getText().compareTo("")==0)){
				JOptionPane.showMessageDialog(frame, "IP  must be filled in order to connect", IP, JOptionPane.ERROR_MESSAGE, null);
				Log.addError("Ip must be filled inorder to execute ping command");
			}else{
				Ping();
			}
		}
		else if (event.getSource() == btnSet)
		{
			
		}
		else if (event.getSource() == btnConnect)
		{
			if ((IPInput.getText().compareTo("")==0) || ((PortInput.getText().compareTo("")==0)))
			{
				
				JOptionPane.showMessageDialog(frame, "IP or Port must be filled in order to connect", IP, JOptionPane.ERROR_MESSAGE, null);
				
			}
			else
			{
				
			
				IP = new String(IPInput.getText());
				PORT = Integer.parseInt((PortInput.getText()));
				//bDidntSetData = false;	
				System.out.println("IP: "+IP+" PORT: "+ PORT);
				
				
		    	//this.dispose();
		    	Log.addError("Conecting to server.... ");
				 
				 bDidntSetConnection = false;
				//panel.setVisible(true);
		    	//panel.setVisible(false);
		    	//panel_1.setVisible(true);
				 clientFrame= new ClientConsole(IP,PORT);
				 MainFrame.frame.setClientRef(clientFrame);
		    	//new Thread(chat).run();
		    	//chat.run();  //Wait for console data
				 if (clientFrame.bConnection){
					 Log.addError("Connected... ");
		    	//this.getParent().remove(this);
		    	//((JFrame) this.getParent()).getContentPane().remove(this);
					 this.setVisible(false);
					 MainFrame.frame.Switch2Console();
				 }
			}
		}
		
    }
	private void Ping(){
		Runtime r = Runtime.getRuntime();
		try {
			Process p = r.exec("ping "+ IPInput.getText());
			
			BufferedReader in = new BufferedReader(new
					InputStreamReader(p.getInputStream()));
					String inputLine;
					Log.ShowLogWindow();
					while ((inputLine = in.readLine()) != null) {
					//System.out.println(inputLine);
						Log.addError(inputLine);
						String pingResult = inputLine;
					}
					in.close();
			
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//catch
	}
	
	public void BuildGui()
	{

		//setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 506, 389);
		setLayout(null);
		
		 panel = new JPanel();
		panel.setBackground(new Color(0, 191, 255));
		panel.setBounds(126, 71, 292, 167);
		//getContentPane().add(panel);
		this.add(panel);
		panel.setLayout(null);
		
		JLabel lblConnection = new JLabel("Connection to Server");
		lblConnection.setBounds(51, 11, 119, 14);
		lblConnection.setBackground(new Color(0, 191, 255));
		panel.add(lblConnection);
		
		 btnConnect = new JButton("Connect");
		btnConnect.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
			}
		});
		btnConnect.setBounds(99, 133, 89, 23);
		panel.add(btnConnect);
		
		IPInput = new JTextField();
		IPInput.setText("localhost");
		//textField.setText("fdfsfds");
		IPInput.setBounds(65, 49, 123, 28);
		panel.add(IPInput);
		IPInput.setColumns(10);
		
		JLabel lblIp = new JLabel("IP:");
		lblIp.setBounds(20, 56, 46, 14);
		panel.add(lblIp);
		
		PortInput = new JTextField();
		PortInput.setText("6001");
		
		PortInput.setBounds(65, 88, 123, 28);
		panel.add(PortInput);
		PortInput.setColumns(10);
		
		JLabel lblPort = new JLabel("Port:");
		lblPort.setBounds(9, 95, 46, 14);
		panel.add(lblPort);
		
		 btnPing = new JButton("Ping");
		btnPing.setBounds(193, 52, 89, 23);
		panel.add(btnPing);
		
		 panel_1 = new JPanel();
		panel_1.setBackground(new Color(0, 255, 127));
		panel_1.setBounds(200, 309, 140, 33);
		this.add(panel_1);
		
		 btnGet = new JButton("Get");
		panel_1.add(btnGet);
		
		 btnSet = new JButton("Set");
		panel_1.add(btnSet);
		panel_1.setVisible(false);
		 btnPing.addActionListener( this);
		btnGet.addActionListener( this);
		btnSet.addActionListener( this);
		btnConnect.addActionListener( this);
		this.setVisible(true);
	}
	@Override
	public void run() {
		// TODO Auto-generated method stub
		
	}
	
	public ClientConsole getClientInstance(){
		return  clientFrame;
		
	}
	public boolean getConnectionStatus(){
		return  bDidntSetConnection;
		
	}
}
